<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<!-- #include file = "../core.asp" -->
<%
	var _system = $('system');
	var _action = _system.read('action');
	var _id = _system.read('id');
	var _parent = _system.read('parent');
	var _type = _system.read('type');
	
	var _database = $('database');
	_database.path = '../database/#database.mdb';
	
	switch (_action) {
		case 'add':
			_add();
			break;
		case 'edit':
			_edit();
			break;
		case 'save':
			_save();
			break;
		case 'delete':
			_delete();
			break;
		case 'update':
			_update();
			break;
		default:
			_main();
			break;
	}
%>

<%
	function _update() {
		var _sql, _url;

		switch (_type) {
			case 'category' : 
				var _name = _system.read('name');
				var _category = _database.query('select [CategoryId] from [Category] where [Id] = ' + _id);
				var _parent = _category[0].categoryId;

				_sql = 'update [Category] set [Name] = \'' + _name + '\' where [Id] = ' + _id;
				_url = 'content.asp?id=' + _parent;
				break;
			case 'article' :
				var _article = _database.query('select [CategoryId] from [Article] where [Id] = ' + _id);
				var _parent = _article[0].categoryId;
				var _title = _system.read('title');
				var _author = _system.read('author');
				var _source = _system.read('source');
				var _content = _system.read('content');
				var _datetime = _system.read('datetime') || _system.now();
				var _image = _system.read('image');
				var _language = _system.read('language');
	
				_sql = 'update [Article] set [Title] = \'' + _title + '\', [Image] = \'' + _image + '\', [Author] = \'' + _author + '\', [Source] = \'' + _source + '\', [Content] = \'' + _content + '\', [Datetime] = \'' + _datetime + '\' where [Id] = ' + _id;
				
				_url = 'content.asp?id=' + _parent
				break;
		}
		//Response.Write(sql);
		//Response.End();
		_database.execute(_sql);
		_system.redirect(_url);
	}
%>

<%
	function _edit() {
		_header();
		switch (_type) {
			case 'category' :
				var _category = _database.query('select [CategoryId], [Name] from [Category] where [Id] = ' + _id);
%>
	<tr>
    	<th colspan="2" class="border"><strong>内容管理 - 编辑类别</strong></th>
    </tr>
    <tr>
   	  	<td valign="top">
       	  	<form action="?action=update&type=category&id=<% =_id %>" method="post">
                <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#999999">
                    <tr>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="name"><strong>名称</strong></label></td>
                        <td bgcolor="#eeeeee">&nbsp;<input type="text" name="name" id="name" value="<% =_category[0].name %>" /></td>
                    </tr>
                    <tr>
                        <td bgcolor="#eeeeee" height="24" colspan="2">&nbsp;<input type="submit" name="submit" id="submit" value="提交" class="button" /></td>
                    </tr>
                </table>
           	</form>
        </td>
    </tr>
            
<%
				break;
			case 'article' :
				var _article = _database.query('select [Title], [Author], [Source], [Content], [Datetime], [Image], [CategoryId] from [Article] where [Id] = ' + _id);
%>
	<tr>
    	<th colspan="2" class="border"><strong>内容管理 - 编辑内容</strong></th>
    </tr>
    <tr>
   	  	<td valign="top">
       	  	<form action="?action=update&type=article&id=<% =_id %>" method="post">
                <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#999999">
                    <tr>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="title"><strong>标题</strong></label></td>
                        <td bgcolor="#eeeeee" colspan="5">&nbsp;<input type="text" name="title" id="title" size="80" value="<% =_article[0].title %>" /></td>
                    </tr>
                    <tr>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="author"><strong>作者</strong></label></td>
                        <td bgcolor="#eeeeee">&nbsp;<input type="text" name="author" id="author" value="<% =_article[0].author %>" /></td>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="source"><strong>来源</strong></label></td>
                        <td bgcolor="#eeeeee">&nbsp;<input type="text" name="source" id="source" value="<% =_article[0].source %>" /></td>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="datetime"><strong>发布时间</strong></label></td>
                        <td bgcolor="#eeeeee">&nbsp;<input type="text" name="datetime" id="datetime" value="<% =_system.format(_article[0].datetime, 'datetime') %>" /></td>
                    </tr>
                    <tr>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="image"><strong>图像</strong></label></td>
                        <td bgcolor="#eeeeee" colspan="5">&nbsp;<input type="text" name="image" id="image" size="80" value="<% =_article[0].image %>" />&nbsp;<a href="javascript: upload('image');">上传</a></td>
                    </tr>
                    <tr>
                        <td bgcolor="#eeeeee" colspan="6">
<% 
				var _editor = $('fckeditor');
				_editor.base = '../fckeditor/';
				_editor.height = '500';
				_editor.value = _article[0].content;
				_editor.create('content');
%>
                        </td>
                    </tr>
                    <tr>
                        <td bgcolor="#eeeeee" height="24" colspan="6">&nbsp;<input type="submit" name="submit" id="submit" value="提交" class="button" /></td>
                    </tr>
                </table>
           	</form>
        </td>
    </tr>
<%
				break;
		}
		_footer();
	}
%>

<%
	function _delete() {
		var _sql, _url;
		switch (_type) {
			case 'category' :
				var _category = _database.query('select [CategoryId] from [Category] where [Id] = ' + _id);
				var _parent = _category[0].categoryId;
				_sql = 'delete * from [Category] where [Id] = ' + _id;
				_url = 'content.asp?id=' + _parent;
						
				break;
			case 'article' :
				var _article = _database.query('select [CategoryId] from [Article] where [Id] = ' + _id);
				var _parent = _article[0].categoryId;
				_sql = 'delete * from Article where Id = ' + _id;
				_url = 'content.asp?id=' + _parent;
				break;
		}

		_database.execute(_sql);
		_system.redirect(_url);
		
	}
%>

<%
	function _save() {
		var _sql, _url;
		switch (_type) {
			case 'category' : 
				if (_id == '') {
					_id = '0'
				}
				
				var _name = _system.read('name');
				_sql = 'insert into [Category]([CategoryId], [Name], [Type]) values (' + _parent + ', \'' + _name + '\', \'Content\')';
				_url = 'content.asp?id=' + _parent;
				break;
			case 'article' : 
				var _title = _system.read('title');
				var _author = _system.read('author');
				var _source = _system.read('source');
				var _content = _system.read('content');
				var _datetime = _system.read('datetime');
				var _image = _system.read('image');
	
				_sql = 'insert into Article([CategoryId], [Title], [Image], [Author], [Source], [Content], [Datetime]) values (' + _parent + ', \'' + _title + '\', \'' + _image + '\', \'' + _author + '\', \'' + _source + '\', \'' + _content + '\', \'' + _datetime + '\')';
				_url = 'content.asp?id=' + _parent;
				break;
		}
		_database.execute(_sql);
		_system.redirect(_url);
	}
%>

<%
	function _add() {
		_header();
		switch (_type) {
			case 'category' :
%>
	<tr>
    	<th colspan="2" class="border"><strong>内容管理 - 添加类别</strong></th>
    </tr>
    <tr>
   	  	<td valign="top">
       	  	<form action="?action=save&type=category&parent=<% =_parent %>" method="post">
                <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#999999">
                    <tr>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="name"><strong>名称</strong></label></td>
                        <td bgcolor="#eeeeee">&nbsp;<input type="text" name="name" id="name" /></td>
                    </tr>
                    <tr>
                        <td bgcolor="#eeeeee" height="24" colspan="2">&nbsp;<input type="submit" name="submit" id="submit" value="提交" class="button" /></td>
                    </tr>
                </table>
           	</form>
        </td>
    </tr>
            
<%
				break;
			case 'article' :
%>
	<tr>
    	<th colspan="2" class="border"><strong>内容管理 - 添加内容</strong></th>
    </tr>
    <tr>
   	  	<td valign="top">
       	  	<form action="?action=save&type=article&parent=<% =_parent %>" method="post">
                <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#999999">
                    <tr>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="title"><strong>标题</strong></label></td>
                        <td bgcolor="#eeeeee" colspan="5">&nbsp;<input type="text" name="title" id="title" size="80" /></td>
                    </tr>
                    <tr>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="author"><strong>作者</strong></label></td>
                        <td bgcolor="#eeeeee">&nbsp;<input type="text" name="author" id="author" /></td>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="source"><strong>来源</strong></label></td>
                        <td bgcolor="#eeeeee">&nbsp;<input type="text" name="source" id="source" /></td>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="datetime"><strong>发布时间</strong></label></td>
                        <td bgcolor="#eeeeee">&nbsp;<input type="text" name="datetime" id="datetime" value="<% =_system.now() %>" /></td>
                    </tr>
                    <tr>
                        <td bgcolor="#eeeeee" height="24" width="80">&nbsp;<label for="image"><strong>图像</strong></label></td>
                        <td bgcolor="#eeeeee" colspan="5">&nbsp;<input type="text" name="image" id="image" size="80" />&nbsp;<a href="javascript: upload('image');">上传</a></td>
                    </tr>
                    <tr>
                        <td bgcolor="#eeeeee" colspan="6">
<% 
				var _editor = $('fckeditor');
				_editor.base = '../fckeditor/';
				_editor.height = '500';
				_editor.create('content');
%>
                        </td>
                    </tr>
                    <tr>
                        <td bgcolor="#eeeeee" height="24" colspan="6">&nbsp;<input type="submit" name="submit" id="submit" value="提交" class="button" /></td>
                    </tr>
                </table>
           	</form>
        </td>
    </tr>
<%
				break;
		}
		_footer();
	}
%>

<%
	function _main() {
		_header();
		if (_type != 'article') {
			var _name;
			var _index = parseInt(_system.read('page')) || 1;
			if (_id == '') {
				_id = '0';
				_name = '根';
			} 
			else {
				if (_id != '0') 
				{
					var _category = _database.query('select [Name] from [Category] where [Id] = ' + _id);
					_name = _category[0].name;
				} 
				else {
					_name = '根';
				}
			}
			var _categories = _database.query('select [Id], [Name] from [Category] where [Type] = \'Content\' and [CategoryId] = ' + _id);
			var _articles = _database.query('[Id], [Title]', 'Article', '[CategoryId] = ' + _id , '[Datetime] desc', '0', _index, 20);
		
%>
	<tr>
    	<th colspan="2" class="border"><strong>内容管理 当前类别:<% =_name %></strong></th>
    </tr>
    <tr>
   	  	<td width="200" valign="top">
       	  	<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#999999">
				<tr>
                    <td bgcolor="#eeeeee" height="24">&nbsp;<strong>类别列表</strong></td>
                    <td bgcolor="#eeeeee" colspan="2" width="81">&nbsp;<a href="?action=add&type=category&parent=<% =_id %>">添加记录</a></td>
				</tr>
<%
			for (var i = 0; i < _categories.length; i++) {
%>
                <tr>
                    <td bgcolor="#eeeeee" height="24">&nbsp;<a href="?type=category&id=<% =_categories[i].id %>"><% =_categories[i].name %></a></td>
                    <td bgcolor="#eeeeee" width="40">&nbsp;<a href="?action=edit&type=category&id=<% =_categories[i].id %>">编辑</a></td>
                    <td bgcolor="#eeeeee" width="40">&nbsp;<a href="?action=delete&type=category&id=<% =_categories[i].id %>" onClick="return confirm('你确定要删除么?');">删除</a></td>
                </tr>
<%
			}
			if (_categories.length == 0) {
%>
				<tr>
					<td bgcolor="#eeeeee" colspan="3" height="24">&nbsp;暂时没有类别！</td>
				</tr>
<%
			}
%>
        	</table>
      	</td>
    	<td valign="top">
        	<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#999999">
				<tr>
                    <td bgcolor="#eeeeee" height="24">&nbsp;<strong>内容列表</strong></td>
                    <td bgcolor="#eeeeee" colspan="2" width="81">&nbsp;<a href="?action=add&type=article&parent=<% =_id %>">添加记录</a></td>
				</tr>
<%
			for (var i = 0; i < _articles.length; i++) {
%>
                <tr>
                    <td bgcolor="#eeeeee" height="24">&nbsp;<a href="?type=article&id=<% =_articles[i].id %>"><% =_articles[i].title %></a></td>
                    <td bgcolor="#eeeeee" width="40">&nbsp;<a href="?action=edit&type=article&id=<% =_articles[i].id %>">编辑</a></td>
                    <td bgcolor="#eeeeee" width="40">&nbsp;<a href="?action=delete&type=article&id=<% =_articles[i].id %>" onClick="return confirm('你确定要删除么?');">删除</a></td>
                </tr>
<%
			}
			if (_articles.length == 0) {
%>
				<tr>
					<td colspan="3" bgcolor="#F4F4F4" height="24">&nbsp;暂时没有内容！</td>
				</tr>
<%
			}
			else {
				var _index = parseInt(_system.read('page')) || 1;
				var _queryString = _system.query;
				var _page = '';
				
				if (_queryString == '' || _queryString.indexOf('page') == 0) {
					_page = '<a href="?' + _queryString.replace(/page=\d+/gi, '') + 'page=' + (_index - 1 > 0 ? _index - 1 : 1) + '">上一页</a>&nbsp;&nbsp;<a href="?' + _queryString.replace(/page=\d+/gi, '') + 'page=' + (_index + 1 < _database.pageCount ? _index + 1 : _database.pageCount) + '">下一页</a>&nbsp;&nbsp;共' + _database.pageCount + '页';
				}
				else {
					_page = '<a href="?' + _queryString.replace(/&page=\d+/gi, '') + '&page=' + (_index - 1 > 0 ? _index - 1 : 1) + '">上一页</a>&nbsp;&nbsp;<a href="?' + _queryString.replace(/&page=\d+/gi, '') + '&page=' + (_index + 1 < _database.pageCount ? _index + 1 : _database.pageCount) + '">下一页</a>&nbsp;&nbsp;共' + _database.pageCount + '页';
				}
%>
				<tr>
					<td colspan="3" bgcolor="#F4F4F4" height="24">&nbsp;<% =_page %></td>
				</tr>
<%
			}
%>
        	</table>
        </td>
    </tr>

<%
		}
		_footer();
	}
%>

<%
//********************************************************************************
%>
<%
	function _header() {
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>BestWeb Content System</title>
<script type="text/javascript" src="../script/jquery.js"></script>
<script type="text/javascript" src="script/validate.js"></script>
<link rel="stylesheet" type="text/css" href="style/content.css" />
</head>

<body>
<script type="text/javascript">
	function upload(id) {
		var image = window.showModalDialog('upload.htm?<% =Math.random() %>', id ,'dialogWidth=400px; dialogHeight=105px');
		if (image != undefined) document.getElementById(id).value = image;

	}
</script>
<table width="100%" border="0" cellspacing="5" cellpadding="0" class="content">
<%
	}
%>

<%
	function _footer() {
%>
</table>
</body>
</html>
<%
	}
%>
<%
//********************************************************************************
%>